package cn.jathlon.datastructure;

import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.map.hash.TObjectIntHashMap;

/**
 * 值是原生int类型的简单双向map实现
 * @author tuxiao.czz
 *
 * @param <K>
 * @param <V>
 */
public class IntBiMap<K> {

	private TObjectIntHashMap<K> forwardMap;
	private TIntObjectHashMap<K> backwardMap;
	
	public IntBiMap(){
		forwardMap = new TObjectIntHashMap<K>();
		backwardMap = new TIntObjectHashMap<K>();
	}
	
	/**
	 * get int value(index) from key.
	 * @param index
	 * @return
	 */
	public int get(K key) { 
		return forwardMap.get(key); 
	}
	
	/**
	 * get Object from int value
	 * @param index
	 * @return
	 */
	public K getKey(int index) { 
		return backwardMap.get(index); 
	}

	/**
	 * put
	 * @param key
	 * @param value
	 */
	public void put(K key, int value) {
		forwardMap.put(key, value);
		backwardMap.put(value, key);
	}

	/**
	 * containing key or not.
	 * @param value
	 * @return
	 */
	public boolean containsKey(K key) { 
		return forwardMap.containsKey(key); 
	}
	
	/**
	 * containing value or not.
	 * @param value
	 * @return
	 */
	public boolean containsValue(int value) { 
		return backwardMap.containsKey(value); 
	}
	
	/**
	 * containing value or not.
	 * @param value
	 * @return
	 */
	public int size() { 
		return backwardMap.size(); 
	}
	
	
	////////
	public static void main(String[] args) {
		
		IntBiMap<String> biMap = new IntBiMap<String>();
		biMap.put("zhangsan", 1);
		biMap.put("lisi", 2);
		biMap.put("wangwu", 3);
		System.out.println(biMap.get("wangwu"));
		System.out.println(biMap.getKey(3));
	}
	
}


